/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.state;

import deatax.GameData;
import deatax.display.gui.DicePanel;
import java.util.ArrayList;
import deatax.Player;
import deatax.card.Card;
import deatax.display.miniboard.MiniBoard;
import deatax.display.miniboard.MiniBoardTile;
import deatax.display.miniboard.MiniMarker;
import deatax.network.Client;
import deatax.network.Messages;
import deatax.network.NetworkListener;
import javax.media.opengl.GLAutoDrawable;
import javax.swing.JPanel;

/**
 *
 * @author pson
 */
public class RollState extends GameState implements NetworkListener {

    JPanel diceContainer;
    int c_roll;
    ArrayList<DicePanel> d_panel;

    public RollState(GameData data) {
        super("Roll", data);

    }

    @Override
    public void begin() {

        Client.get().addListener(this);
        if (!data.getCurrentPlayer().beginTurn()) {
            data.getGui().setPlayerInfo(data.getCurrentPlayer());
            data.getGui().setLabelMode(data.getCurrentPlayer().getName() + "'s Turn");
            data.getTray().setPlayer(data.getCurrentPlayer());

            data.getGui().enableEndTurn(false);
            data.getGui().refreshContent();

            diceContainer = new JPanel();
            diceContainer.setOpaque(false);

            c_roll = 0;
            //data.getGui().addComponent(diceContainer, 4);
            new DiceRollThread(10).start();
        }
    }

    @Override
    public void display(GLAutoDrawable glad) {

        super.display(glad);
//        data.getGui().updateDice();
    }

    public void endRoll() {
        new BoardMoveThread().start();
    }

    @Override
    public void finish() {
        Client.get().removeListener(this);
        //data.getGui().removeComponent(diceContainer, 4);
        data.getState().popState();
        data.getState().pushState("Turn");
    }

    @Override
    public void processMessage(int type, String[] arg) {
        commonMessages(type, arg);
        switch (type) {

            case Messages.LOSER: {

                String pName = arg[0];
                Player p = data.getRoster().getPlayer(pName);
                if (p == data.getCurrentPlayer()) {
                    Client.get().removeListener(this);
                    data.nextPlayerTurn();
                    data.getState().popState();
                    if (data.getSelf() == p) {
                        data.getState().pushState("Lose");
                    } else {
                        data.getState().pushState("Roll");
                    }
                }
            }
            break;
            case Messages.WINNER: {
                Client.get().removeListener(this);
                data.getState().popState();
                data.getState().pushState("Win");
            }
            break;
            default:
                break;
        }
    }

    private class DiceRollThread extends Thread {

        private int length;

        public DiceRollThread(int length) {
            this.length = length;
        }

        @Override
        public void run() {
            data.getDice().newRoll();
            try {
//                for (int i = 0; i < length; i++) {
                c_roll = data.getDice().roll();
                data.getMiniboard().setDice(data.getDice().current());
                Thread.sleep(500);
//                }

                Player p = data.getCurrentPlayer();
                data.getMiniboard().getMarker(p).setTarget(c_roll);
                data.getGui().addMessage(p, "Rolls a" + ((c_roll == 8 || c_roll == 11) ? "n " : " ") + c_roll);
                //p.moveMbPosition(c_roll);
                //p.setMbPosition((p.getMbPosition()+c_roll)%data.getMiniboard().size());

                endRoll();
            } catch (InterruptedException ex) {
            }
        }
    }

    private class BoardMoveThread extends Thread {

        public BoardMoveThread() {
        }

        @Override
        public void run() {
            MiniBoard mb = data.getMiniboard();
            Player p = data.getCurrentPlayer();
            MiniMarker mm = mb.getMarker(p);
            try {
                while (mm.onTheMove) {
                    Thread.sleep(500);
                }

                MiniBoardTile mbt = mb.getTile(mm.getPosition());
                if (mbt.getType() != null) {
                    Card c = data.getCards().drawCard(mbt.getType());
                    mb.animateCard(c);
                    Thread.sleep(5000);
                    mb.clearCard();
                    c.playCard(data);

                    data.getGui().refreshContent();
                }
                finish();
            } catch (InterruptedException ex) {
            }

        }
    }
}
